// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// This file sits in: docs/content/sidebars/
// Content root is one level up from here:
const CONTENT_ROOT = path.resolve(__dirname, '..');

function dirExists(rel) {
  return fs.existsSync(path.join(CONTENT_ROOT, rel));
}

const alphaDir = 'references/sui-api/sui-graphql/alpha/reference';
const betaDir  = 'references/sui-api/sui-graphql/beta/reference';

const graphqlAlpha = {
  type: 'category',
  label: 'Alpha',
  collapsed: false,
  // Having a link makes the category valid even if items are absent
  link: { type: 'generated-index', slug: `/${alphaDir}` },
  items: dirExists(alphaDir) ? [{ type: 'autogenerated', dirName: alphaDir }] : [],
};

const graphqlBeta = {
  type: 'category',
  label: 'Beta',
  collapsed: false,
  link: { type: 'generated-index', slug: `/${betaDir}` },
  items: dirExists(betaDir) ? [{ type: 'autogenerated', dirName: betaDir }] : [],
};

const references = [
	{
		type: 'doc',
		label: 'References',
		id: 'references',
	},
	{
		type: 'category',
		label: 'Sui RPC',
		collapsed: false,
		link: {
			type: 'doc',
			id: 'references/sui-api',
		},
		items: [
			{
				type: 'category',
				label: 'GraphQL',
				link: {
					type: 'doc',
					id: 'references/sui-graphql',
				},
				items: [
					graphqlBeta,
					graphqlAlpha,
				],
			},
			{
				type: 'link',
				label: 'JSON-RPC',
				href: '/sui-api-ref',
			},
			'references/fullnode-protocol',
			'references/sui-api/rpc-best-practices',
		],
	},
	{
		type: 'category',
		label: 'Sui CLI',
		collapsed: false,
		link: {
			type: 'doc',
			id: 'references/cli',
		},
		items: [
			'references/cli/cheatsheet',
			'references/cli/client',
			'references/cli/ptb',
			'references/cli/keytool',
			'references/cli/move',
      		'references/cli/replay',
			'references/cli/trace-analysis',
			'references/cli/validator',
		],
	},
	{
		type: 'category',
		label: 'Sui IDE Support',
		collapsed: false,
		items: [
			'references/ide/move',
			'references/ide/debugger',
		],
	},
	{
		type: 'category',
		label: 'Sui SDKs',
		collapsed: false,
		link: {
			type: 'doc',
			id: 'references/sui-sdks',
		},
		items: [
			{
				type: 'link',
				label: 'dApp Kit',
				href: 'https://sdk.mystenlabs.com/dapp-kit',
			},
			'references/rust-sdk',
			{
				type: 'link',
				label: 'TypeScript SDK',
				href: 'https://sdk.mystenlabs.com/typescript',
			},
			{
				type: 'link',
				label: 'zkSend SDK',
				href: 'https://sdk.mystenlabs.com/zksend',
			},
		],
	},
	{
		type: 'category',
		label: 'Move',
		collapsed: false,
		link: {
			type: 'doc',
			id: 'references/sui-move',
		},
		items: [
			{
				type: 'category',
				label: 'Framework',
				link: {
					type: 'doc',
					id: 'references/framework',
				},
				items: [{ type: 'autogenerated', dirName: 'references/framework' }],
			},
			{
				type: 'link',
				label: 'The Move Book',
				href: 'https://move-book.com/',
			},
			{
				type: 'link',
				label: 'The Move Reference',
				href: 'https://move-book.com/reference/',
			},
		],
	},
	'references/awesome-sui',
	'references/sui-glossary',
	{
		type: 'category',
		label: 'Contribute',
		link: {
			type: 'doc',
			id: 'references/contribute/contribution-process',
		},
		items: [
			'references/contribute/sui-environment',
			'references/contribute/contribute-to-sui-repos',
			{
				type: 'link',
				label: 'Submit a SIP',
				href: 'https://sips.sui.io',
			},
			'references/contribute/localize-sui-docs',
			'references/contribute/code-of-conduct',
			'references/contribute/style-guide',
			'references/contribute/mdx-components',
		],
	},
];

export default references;
